/**
 * Created by Zfy on 2015/11/3.
 */
function addCity(id,name){
    $("#al_add").empty();
    $("#city_checks").empty();
    $("#city_moadl_error").empty();
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"id":id},
        url:"/index.php?r=rbac/getcitys",
        success:function(data) {
            var data=eval(data);
            for(var i=0;i<data.length;i++){
                $("#al_add").append("<a href='#' class='icon-btn span2' id='a_"+data[i]['id']+"'>"+data[i]['name']+"<span class='badge badge-important' onclick='deleCity(\""+data[i]['id']+"\")'>×</span></a>");
            }
            $("#area_id1").val(id);
            $("#area_name1").val(name);
            $("#addCity").modal();
        }
    });
}
function newcity(){
    var area_id= $("#area_id1").val();
    $("#city_moadl_error").empty();
    var name=$("#city_name").val();
    if(name==""||name==null){
        $("#city_moadl_error").html("<span style='color: red'>&nbsp;&nbsp;&nbsp;添加城市名不能为空！</span>");
        return false;
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"name":name,"area_id":area_id},
        url:"/index.php?r=rbac/addcity",
        success:function(data) {
            $("#city_moadl_error").empty();
            if(data){
                var data=eval(data);
                if(data['state']==1){
                    $("#al_add").append("<a href='#' class='icon-btn span2' id='a_"+data['city_id']+"'>"+name+"<span class='badge badge-important' onclick='deleCity(\""+data['city_id']+"\")'>×</span></a>");
                    $("#city_name").val('');
                }else{
                    $("#city_moadl_error").html("<span style='color: red'>&nbsp;&nbsp;&nbsp;"+data['str']+"！</span>");
                }
            }
        }
    });
}
function deleCity(id){
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"id":id},
        url:"/index.php?r=rbac/delcity",
        success:function(data) {
            $("#city_moadl_error").empty();
            if(data){
                var data=eval(data);
                if(data['state']==1){
                    $("#a_"+id).remove();
                }else{
                    $("#city_moadl_error").html("<span style='color: red'>&nbsp;&nbsp;&nbsp;"+data['str']+"！</span>");
                }
            }
        }
    });
}
function backArea(){
    location.reload();
}
function addArea(){
    $("#biaoti").html("新建大区");
    $("#area_name").val("");
    $("#area_mark").val("");
    $("#area_id3").val("");
    $("#areaFrom"). attr("action","/index.php?r=rbac/addarea");
    $("#addArea").modal();
    $("#area_click").click(function(){
        submitCheck(0);
    });
}
function updateArea(id,name,remark){
    $("#area_name").val(name);
    $("#area_mark").val(remark);
    $("#area_id3").val(id);
    $("#areaFrom"). attr("action","/index.php?r=rbac/updatearea");
    $("#addArea").modal();
    $("#biaoti").html("编辑大区");
    $("#area_click").click(function(){
        submitCheck(id);
    });
}
function submitCheck(id){
    $("#error_check").empty();
    var name=$("#area_name").val();
    if(name==''||name==null){
        $("#error_check").append("<p style='color: red'>大区名不能为空</p>");
        return false;
    }else{
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"name":name},
            url:"/index.php?r=rbac/getareaname",
            success:function(data) {
                if(data==id||data==0){
                    $("#areaFrom").submit();
                }else{
                    if(data=="操作失败"){
                        $("#error_check").append("<p style='color: red'>操作失败</p>");
                        return false;
                    }else{
                        $("#error_check").append("<p style='color: red'>已有同名大区</p>");
                        return false;
                    }
                }
            }
        });
    }
}
function deleteModal(id){
    $("#delete_moadl_error").empty();
    $("#deleteModal").modal();
    $("#delete_click").click(function(){
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"id":id},
            url:"/index.php?r=rbac/delarea",
            success:function(data) {
                $("#delete_moadl_error").empty();
                if(data){
                    var data=eval(data);
                    if(data['state']==1){
                       location.reload();
                    }else{
                        $("#delete_moadl_error").html("<span style='color: red'>&nbsp;&nbsp;&nbsp;"+data['str']+"！</span>");
                    }
                }
            }
        });
    });

}